package com.baozun.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baozun.mapper.TAppendixMapper;
import com.baozun.model.TAppendix;
import com.baozun.model.TAppendixExample;
import com.baozun.service.AppendixService;
import com.baozun.util.PageData;

@Service("appendixService")
@Transactional
public class AppendixServiceImpl implements AppendixService {

    @Autowired
    private TAppendixMapper appendixMapper;

    @Override
    public void add(TAppendix bean) {
        // TODO Auto-generated method stub
        if (StringUtils.isEmpty(bean.getStAppendixId())) {
            bean.setStAppendixId(UUID.randomUUID().toString());
        }
        bean.setDtCreateTime(new Date());
        appendixMapper.insert(bean);
    }

    @Override
    public TAppendix get(String id) {
        // TODO Auto-generated method stub
        return appendixMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(TAppendix bean) {
        // TODO Auto-generated method stub
        bean.setDtUpdateTime(new Date());
        appendixMapper.updateByPrimaryKeySelective(bean);
    }

    @Override
    public void delete(String stAppendixId) {
        // TODO Auto-generated method stub
        appendixMapper.deleteByPrimaryKey(stAppendixId);
    }

    @Override
    public PageData<TAppendix> getPageListByExample(TAppendixExample record, int pageSize, int currentPage) {
        // TODO Auto-generated method stub
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TAppendix> infoList = new PageData<TAppendix>();
        List<TAppendix> appendixList = appendixMapper.selectByExample(record);
        int count = appendixMapper.countByExample(record);
        infoList.addAll(appendixList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }

    @Override
    public List<TAppendix> getList(TAppendixExample record) {
        // TODO Auto-generated method stub
        return appendixMapper.selectByExample(record);
    }
}
